package com.baidu.sggc.controller;

import com.baidu.sggc.core.common.R;
import com.baidu.sggc.audit.extend.AuditExtendContext;
import com.baidu.sggc.audit.extend.GeneralMessage;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.baidu.sggc.core.annotation.AuditLog;
import com.baidu.sggc.data.req.SolutionInfoCreateReq;
import com.baidu.sggc.data.req.SolutionInfoListReq;
import com.baidu.sggc.data.req.SolutionInfoUpdateReq;
import com.baidu.sggc.data.resp.Page;
import com.baidu.sggc.entity.SolutionInfo;
import com.baidu.sggc.service.SolutionInfoService;
import com.baidu.sggc.entity.vo.SolutionInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;

@Controller
@RequestMapping("/solution")
@Tag(name = "SolutionInfoController", description = "解决方案控制器")
public class SolutionInfoController {

  @Autowired
  private SolutionInfoService solutionInfoService;

  @Operation(summary = "获取解决方案列表")
  @PostMapping("/list")
  @ResponseBody
  @AuditLog(module = "解决方案", auditObject = "方案", operation = "listQuery", message = "获取解决方案列表")
  public R<Page<SolutionInfoVo>> list(@RequestBody SolutionInfoListReq req) {
    req.validate();

    Map<String, Map<String, String>> extendData = new LinkedHashMap<String, Map<String, String>>();
    Map<String, String> condition = new LinkedHashMap<String, String>();
    condition.put("名称", req.getKeyWord());
    condition.put("标签", req.getKeyWord());
    extendData.put("查询条件", condition);
    AuditExtendContext.setExtendData((Map) extendData);

    return R.ok(solutionInfoService.getSolutionInfoList(req));
  }

  @Operation(summary = "根据id查询解决方案信息")
  @GetMapping("/detail")
  @ResponseBody
  @AuditLog(module = "解决方案", auditObject = "方案", operation = "detailsQuery", message = "查询方案详情")
  public R<SolutionInfoVo> get(Long id) {
    if (id == null) {
      throw new IllegalArgumentException("id不能为空");
    }

    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("解决方案");
    msg.setDataName("方案ID: " + id);
    msg.setUniqueIdentification(Collections.singletonList(String.valueOf(id)));
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok(solutionInfoService.getSolutionInfoById(id));
  }

  @Operation(summary = "根据id编辑解决方案")
  @PostMapping("/update")
  @ResponseBody
  @AuditLog(module = "解决方案", auditObject = "方案", operation = "update", message = "编辑解决方案")
  public R<Boolean> update(@RequestBody SolutionInfoUpdateReq req) {
    req.validate();

    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("解决方案");
    msg.setDataName(req.getSolutionName());
    msg.setUniqueIdentification(Collections.singletonList(String.valueOf(req.getId())));
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok(solutionInfoService.updateSolutionInfoById(req));
  }

  @Operation(summary = "创建解决方案")
  @PostMapping("/add")
  @ResponseBody
  @AuditLog(module = "解决方案", auditObject = "方案", operation = "create", message = "创建解决方案")
  public R<Boolean> add(@RequestBody SolutionInfoCreateReq req) {
    Long solutionId = solutionInfoService.createSolutionInfo(req);

    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("解决方案");
    msg.setDataName(req.getSolutionName());
    msg.setUniqueIdentification(Collections.singletonList(String.valueOf(solutionId))); // 用主键ID
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok(true);
  }

  @Operation(summary = "删除解决方案")
  @PostMapping("/del")
  @ResponseBody
  @AuditLog(module = "解决方案", auditObject = "方案", operation = "delete", message = "删除解决方案")
  public R<Boolean> del(@RequestBody SolutionInfo req) {
    if (req.getId() == null) {
      throw new IllegalArgumentException("id不能为空");
    }

    GeneralMessage msg = new GeneralMessage();
    msg.setDataType("解决方案");
    msg.setDataName(req.getSolutionName());
    msg.setUniqueIdentification(Collections.singletonList(String.valueOf(req.getId())));
    msg.setOperationNum(1);
    AuditExtendContext.setExtendData(toMap(msg));

    return R.ok(solutionInfoService.deleteSolutionInfo(req));
  }

  private Map<String, Object> toMap(Object obj) {
    ObjectMapper mapper = new ObjectMapper();
    return mapper.convertValue(obj, Map.class);
  }
}
